const app = getApp()
import util from '../../../utils/util'
import {
  GetGriderEventList,
  GetWsxfList,
  GetIsseuChuLiEventList,
  GetShiveList,
  GetIsseuGriderList,
  GetPhoneAppealList
} from '../../../config/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: '', // 下级处理type
    isEmpty: false,
    grider: '', // 网格员信息
    cat_id: '3', // 2走访排查3村民上报
    grider_id:0,//筛选下级人员id
    status:'',//1已解决2正在处理0待解决
    time:'',//月份
    ucid:0,//事件类型
    page: 1,
    limit: 10,
    lists: [],
    uc_name:"全部类型",
    showView:true,
    ucList:[],//事件类型数组
    status_name:"全部状态",
    showViewStatus:true,
    isseuGriderList:[],//下级列表
    isseuGrider:[],
    showGriderView:true,
    griderChageIndex:0,
    grider_idc:0

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    let grider = wx.getStorageSync('grider');
    // grider={
    //   id:8
    // }
    this.setData({
      grider: grider,
      grider_id:grider.id,
      grider_idc:grider.id
    })
    this.getYearMonth();
    if (this.data.cat_id == 99) {
      this.getPetitionList()
    } else {
      this.getList()
    }
    this.getIsseuGriderList(this.data.grider_id);
  },

  getList() { // 获取列表
    let that = this;

    wx.showLoading({
      title: '玩命加载中',
    })

    let params = {
      id:this.data.grider.id,
      vollege_id: app.globalData.vollege_id,
      grider_id:this.data.grider_id,
      time:this.data.time,
      page: this.data.page,
      limit: this.data.limit
    }
    util.apiRequest(GetPhoneAppealList, params, null).then(r => {
      console.log('电话诉求列表', r)

      var newList = that.data.lists.concat(r.data.data.data)
      if(r.data.code==0){
        wx.showToast({
          title: r.data.info,
          icon: 'none',
          duration:2000
        })
        that.setData({
          isEmpty: true
        })
        return false;
      }
      if (r.data.data.data == '') {
        that.setData({
          lists: newList,
          isBottom: true
        })
        wx.showToast({
          title: '暂无更多',
          icon: 'none',
        })
      } else {
        that.setData({
          lists: newList
        })
      }
      wx.hideLoading();

      if (newList.length == 0) {
        that.setData({
          isEmpty: true
        })
      } else {
        that.setData({
          isEmpty: false
        })
      }



    })
  },


  //日期选择事件
  bindDateChange(e){
    console.log("日期选择事件",e)
    let time = e.detail.value
    this.setData({
      time:time,
      lists:[],
      page:1,
    })
    this.getList();
  },
  //调用下级人员
  getIsseuGriderList(id){
    let that=this;
    util.apiRequest(GetIsseuGriderList, {id:id}, null).then(r => {
      console.log('下级人员1', r)
      if(r.data.data.length>0){
        let isseuGriderData=that.data.isseuGrider
        let data = {
          id:0,
          name:"全部人员"
        }
        console.log('isseuGriderData1',isseuGriderData)
        console.log('isseuGrider1',data)
        isseuGriderData.push(data)
        that.setData({
          isseuGriderList:r.data.data,
          isseuGrider:isseuGriderData
        })
      }
    })
  },

  //弹出选择网格员遮罩层
  isseuGriderChage(e){
    let that = this
    let griderChageIndex = e.currentTarget.dataset.index
    let id = 0;
    console.log("that.grider",that.data.grider)
    if(griderChageIndex==0){
      id=that.data.grider.id
    }else{
      id=that.data.isseuGrider[griderChageIndex-1].id
    }
    util.apiRequest(GetIsseuGriderList, {id:id}, null).then(r => {
      console.log('下级人员1', r)
      if(r.data.data.length>0){
        that.setData({
          isseuGriderList:r.data.data,
          showGriderView:false,
          griderChageIndex:griderChageIndex
        })
      }
    })
  },

  //网格员选择确定
  griderSetm(e){
    let that = this;
    let id = e.currentTarget.dataset.id
    let name = e.currentTarget.dataset.title
    let griderChageIndex=that.data.griderChageIndex;
    let isseuGrider = that.data.isseuGrider;
    let grider_id = id
    isseuGrider[griderChageIndex]={
      id:id,
      name:name
    }
    if(id==0){
      if(griderChageIndex-1<0){
        grider_id=that.data.grider.id
      }else{
        grider_id=isseuGrider[griderChageIndex-1].id
      }
      
    }
    util.apiRequest(GetIsseuGriderList, {id:id}, null).then(r => {
      console.log('下级人员1', r)
      if(r.data.data.length>0){
        let data = {
          id:0,
          name:"全部人员"
        }
        let isseuGriderNew = [];
        for(var i=0;i<=griderChageIndex;i++){
          isseuGriderNew[i]=isseuGrider[i]
        }
        console.log("isseuGriderNew",isseuGriderNew)
        isseuGriderNew[griderChageIndex+1]=data
        console.log("isseuGriderNew111",isseuGriderNew)
        that.setData({
          isseuGriderList:r.data.data,
          isseuGrider:isseuGriderNew,
        })

      }else{
        let isseuGriderNew = [];
        for(var i=0;i<=griderChageIndex;i++){
          isseuGriderNew[i]=isseuGrider[i]
        }
        that.setData({
          isseuGrider:isseuGriderNew,
        })
      }
      that.setData({
        showGriderView:true,
        grider_id:grider_id,
        grider_idc:id,
        lists:[]
      })
      console.log("isseuGrider111",that.data.isseuGrider)
      that.getList();
    })

  },

  getYearMonth() { // 计算当前获取年月日
    var myDate = new Date();
    var tYear = myDate.getFullYear();
    var tMonth = myDate.getMonth();
    var tday = myDate.getDate()

    var m = tMonth + 1;
    if (m.toString().length == 1) {
      m = "0" + m;
    }

    if (tday.toString().length == 1) {
      tday = "0" + tday;
    }

    let endDate = tYear + '-' + m + '-' + tday;
    let month = tYear + '-' + m;

    console.log('当前日期', endDate)
    this.setData({
      time:month,
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    var page = this.data.page;
    page++;
    that.setData({
      page: page
    })

    console.log('page++', page)

    if (this.data.cat_id == 99) {
      this.getPetitionList()
    } else {
      this.getList()
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})